package com.webex.ta.hydra.core.assertion;

import com.webex.ta.hydra.core.*;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-10-13
 * Time: 16:35:13
 */
public class AssertResponse extends AbstractAssertion {
    private static final String RESPONSE_BODY = "response body";
    private static final String RESPONSE_CODE = "response code";
    private static final String RESPONSE_HEADER = "response headers";

    private static final String CONTAINS = "contains";
    private static final String EQUALS = "equals";
    private static final String MATCHES = "matches";
    private static final String NOT_CONTAINS = "not_contains";
    private static final String NOT_EQUALS = "not_equals";
    private static final String NOT_MATCHES = "not_matches";

    private static final ParamDescriptor[] desc = new ParamDescriptor[3];

    static {
        desc[0] = new ParamDescriptor(ParamType.LIST, "test field", false,
                new String[]{RESPONSE_BODY, RESPONSE_CODE, RESPONSE_HEADER});
        desc[1] = new ParamDescriptor(ParamType.LIST, "compare rule", false,
                new String[]{CONTAINS, EQUALS, MATCHES, NOT_CONTAINS, NOT_EQUALS, NOT_MATCHES});
        desc[2] = new ParamDescriptor("text to test");
    }

    public ParamDescriptor[] getParamDescriptors(Command cmd) {
        return desc;
    }

    public void doAssertion(TestResult tr, AssertionResult ar, String... args) {
        String toCheck = tr.getResponse();
        String field = args[0];
        if (RESPONSE_CODE.equals(field)) {
            toCheck = tr.getResponseCode();
        } else if (RESPONSE_HEADER.equals(field)) {
            toCheck = tr.getRequestHeaders();
        }

        String pattern = args[2];
        String type = args[1];
        boolean found;
        if (CONTAINS.equals(type) || NOT_CONTAINS.equals(type)) {

        } else if (EQUALS.equals(type) || NOT_EQUALS.equals(type)) {
            found = toCheck.equals(pattern);
            if (found && NOT_EQUALS.equals(type)) {
                ar.setFailure(true);
            } else if (EQUALS.equals(type) && !found) {
                ar.setFailure(true);
            }
        } else if (MATCHES.equals(type) || NOT_MATCHES.equals(type)) {

        }

        if (ar.isFailure()) {
            ar.setMessage(getFailureMessage(pattern, field, type));
        }
    }

    private String getFailureMessage(String pattern, String field, String type) {
        StringBuffer sb = new StringBuffer(200);
        sb.append("Assertion failed: ");
        if (RESPONSE_CODE.equals(field)) {
            sb.append("code ");
        } else if (RESPONSE_HEADER.equals(field)) {
            sb.append("headers ");
        }
        sb.append("expected ");
        if (EQUALS.equals(type)) {
            sb.append("to equal ");
        } else if (NOT_EQUALS.equals(type)) {
            sb.append("not to equal ");
        } else if (CONTAINS.equals(type)) {
            sb.append("to contain ");
        } else if (NOT_CONTAINS.equals(type)) {
            sb.append("not to contain ");
        } else if (MATCHES.equals(type)) {
            sb.append("to match ");
        } else if (NOT_MATCHES.equals(type)) {
            sb.append("not to match ");
        }
        sb.append("/");
        sb.append(pattern);
        sb.append("/");
        return sb.toString();
    }
}
